package com.qianxx.component.activity.progress.circular_progress_button;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;
import com.qianxx.viewlibrary.progress.circularprogressbutton.CircularProgressButton;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CircularProgressButtonActivity extends BaseActivity {
    @BindView(R.id.circular_button)
    CircularProgressButton circular_button;

    DownLoadThread mThread;
    boolean isStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circular_progress_button);
        ButterKnife.bind(this);

        circular_button.setIndeterminateProgressMode(true);
        circular_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mThread == null){
                    mThread = new DownLoadThread();
                    mThread.start();
                }
            }
        });

    }

    private class DownLoadThread extends Thread{
        @Override
        public void run() {
            for (int i = 0; i <= 100; i++) {
                if(isStop)
                    return;
                handler.sendEmptyMessage(i);
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            int progress = msg.what;
            if (circular_button.getProgress() == 100) {
                circular_button.setProgress(0);
            } else {
                circular_button.setProgress(progress);
            }
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        isStop = true;
    }

    @Override
    public String setTitle() {
        return "CircularProgressButton";
    }
}
